package com.wangyi.im.remoting;

import com.alibaba.fastjson.JSONObject;
import com.wangyi.im.execute.IMTokenExecute;
import com.wangyi.im.request.IMTokenRequest;
import com.wangyi.im.response.IMResponse;
import com.wangyi.im.response.IMTokenResponse;
import com.wangyi.im.utils.BuildDataUtil;
import retrofit2.Call;
import retrofit2.Response;
import static com.wangyi.im.constants.CommonConstants.INFO;

import java.io.IOException;

public class IMTokenRemoting extends Remoting {

    public IMTokenRemoting(String baseUrl, String appKey, String appSecret) {
        super(baseUrl, appKey, appSecret);
    }

    public IMResponse<IMTokenResponse> createIMID(IMTokenRequest imTokenRequest){
        Call<JSONObject> call = getRetrofit(imTokenRequest).create(IMTokenExecute.class).create();
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BuildDataUtil().buildData(retrofitResponse, IMTokenResponse.class, INFO);
    }

    public IMResponse<IMTokenResponse> block(IMTokenRequest imTokenRequest){
        Call<JSONObject> call = getRetrofit(imTokenRequest).create(IMTokenExecute.class).block();
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BuildDataUtil().buildData(retrofitResponse, IMTokenResponse.class, INFO);
    }

    public IMResponse<IMTokenResponse> unblock(IMTokenRequest imTokenRequest){
        Call<JSONObject> call = getRetrofit(imTokenRequest).create(IMTokenExecute.class).unblock();
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BuildDataUtil().buildData(retrofitResponse, IMTokenResponse.class, INFO);
    }

    public IMResponse<IMTokenResponse> updateToken(IMTokenRequest imTokenRequest){
        Call<JSONObject> call = getRetrofit(imTokenRequest).create(IMTokenExecute.class).updateToken();
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BuildDataUtil().buildData(retrofitResponse, IMTokenResponse.class, INFO);
    }

    public IMResponse<IMTokenResponse> refreshToken(IMTokenRequest imTokenRequest){
        Call<JSONObject> call = getRetrofit(imTokenRequest).create(IMTokenExecute.class).refreshToken();
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BuildDataUtil().buildData(retrofitResponse, IMTokenResponse.class, INFO);
    }
}
